package com.hsm.controller;

import com.hsm.constant.MessageConstant;
import com.hsm.constant.RedisConstant;
import com.hsm.entity.PageResult;
import com.hsm.entity.QueryPageBean;
import com.hsm.entity.Result;
import com.hsm.pojo.CheckGroup;
import com.hsm.pojo.Setmeal;
import com.hsm.service.CheckGroupService;
import com.hsm.service.SetMealService;
import com.hsm.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {

    @Autowired
    private SetMealService setMealService;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 新增套餐
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(Integer[] checkgroupIds, @RequestBody Setmeal setmeal){
        try {
            setMealService.add(checkgroupIds,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        // 点击了添加按钮，则将图片信息保存到数据库中
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile){
        try {
            //获取原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            int lastIndexOf = originalFilename.lastIndexOf(".");
            //获取文件后缀
            String suffix = originalFilename.substring(lastIndexOf);

            //使用UUID随机产生文件名称，防止同名文件覆盖
            String filename = UUID.randomUUID().toString() + suffix;

            // 上传图片
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),filename);

            //图片上传成功
            Result result = new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,filename);

            //将上传图片名称存入Redis，基于Redis的Set集合存储
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,filename);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 分页查询套餐数据
     * @return
     */
    @RequestMapping(value = "/findPage",method = RequestMethod.POST)
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            Integer pageSize = queryPageBean.getPageSize();
            Integer currentPage = queryPageBean.getCurrentPage();
            String queryString = queryPageBean.getQueryString();
            PageResult pageResult = setMealService.findPage(pageSize,currentPage,queryString);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 删除套餐数据
     * @return
     */
    @RequestMapping(value = "/deleteSetMeal",method = RequestMethod.GET)
    public Result deleteSetMeal(Integer setMealId){
        try {
            setMealService.deleteCheckGroup(setMealId);

            return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }


    }

    /**
     * 根据id查询套餐信息  进行数据回显
     * @param setMealId
     * @return
     */
    @RequestMapping(value = "/findSetMealById",method = RequestMethod.POST)
    public Result findSetMealById(Integer setMealId){
        Result result = null;
        try {
            Setmeal setmeal = setMealService.findSetMealById(setMealId);
            result = new Result(true,"数据回显成功",setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false,"数据回显失败");
        }
        return result;
    }

    /**
     * 通过检查组的id 查询中间表中有多少检查项，并进行回填打勾
     * @param setMealId
     * @return
     */
    @RequestMapping(value = "/findCheckgroupIdsBySetMealId",method = RequestMethod.POST)
    public List<Integer> findCheckgroupIdsBySetMealId(Integer setMealId){
        List<Integer> list = null;
        try {
            list = setMealService.findCheckgroupIdsBySetMealId(setMealId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return list;
    }


    /**
     * 修改套餐信息
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Result edit(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        Result result = null;
        try {
            setMealService.edit(setmeal,checkgroupIds);
            result = new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
        return result;
    }

}
